package com.ybl.store.backend.controller.deal;

import com.alibaba.excel.EasyExcel;
import com.ybl.store.backend.dto.req.deal.DealProductSaveReqDTO;
import com.ybl.store.backend.dto.req.deal.DealProductUpdateReqDTO;
import com.ybl.store.backend.dto.resp.deal.DealProductExportRespDTO;
import com.ybl.store.backend.dto.resp.deal.DealProductPageRespDTO;
import com.ybl.store.backend.service.deal.DealProductService;
import com.ybl.store.common.convention.errorcode.BaseErrorCode;
import com.ybl.store.common.convention.exception.ClientException;
import com.ybl.store.common.convention.result.Result;
import com.ybl.store.common.convention.result.Results;
import com.ybl.store.common.entity.ChildrenNameId;
import com.ybl.store.common.entity.ListRespDTO;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/backend/deal/product")
public class DealProductController {

    private final DealProductService dealProductService;

    @PostMapping
    public Result<Void> saveDealProduct(@RequestBody DealProductSaveReqDTO requestParam){
        dealProductService.saveDealProduct(requestParam);
        return Results.success();
    }

    @DeleteMapping
    public Result<Void> deleteDealProduct(@RequestParam Long id) {
        dealProductService.deleteDealProduct(id);
        return Results.success();
    }

    @PutMapping
    public Result<Void> updateDealProduct(@RequestBody DealProductUpdateReqDTO requestParam){
        dealProductService.updateDealProduct(requestParam);
        return Results.success();
    }

    @GetMapping("/page")
    public Result<ListRespDTO<DealProductPageRespDTO>> pageDealProduct(
            @RequestParam("pn") Integer pn,
            @RequestParam("ps") Integer ps,
            @RequestParam(required = false) Long storeId,
            @RequestParam(required = false) Long clerkId,
            @RequestParam(required = false) Long customerId,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) String startTime,
            @RequestParam(required = false) String endTime,
            @RequestParam(required = false) Integer dealStatus)
    {
        return Results.success(dealProductService.pageDealProduct(pn, ps, storeId, clerkId, customerId, productId, startTime, endTime, dealStatus));
    }

    @GetMapping("/cascder")
    public Result<Map<String, Object>> Cascder(){
        Map<String, Object> map = new HashMap<>();
        map.put("nameIdList",dealProductService.Cascader());

        return Results.success(map);
    }

//    public void export(HttpServletResponse response) {
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setHeader("Content-Disposition", "attachment; filename=data.xlsx");
//
//        try {
//            List<DealProductExportRespDTO> export = dealProductService.export();
//            EasyExcel.write(response.getOutputStream(), DealProductExportRespDTO.class)
//                    .sheet("data")
//                    .doWrite(export);
//        }
//        catch (IOException e) {
//            throw new ClientException(BaseErrorCode.EXCEL_ERROR);
//        }
//    }
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=data.xlsx");

        try {
            List<DealProductExportRespDTO> export = dealProductService.export();
            EasyExcel.write(response.getOutputStream(), DealProductExportRespDTO.class)
                    .sheet("data")
                    .doWrite(export);
        }
        catch (IOException e) {
            throw new ClientException(BaseErrorCode.EXCEL_ERROR);
        }
    }
}
